package com.resource.collect.common;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

@ApiModel("返回数据结果")
public class Result<T> {
	@ApiModelProperty(value="提示信息")
	private String message;
	@ApiModelProperty(value="状态码")
	private int code;
	@ApiModelProperty(value="数据")
	private T data;

	private Result(T data) {
		this.code = 200;
		this.message = "成功";
		this.data = data;
	}

	private Result(CodeMsg cm) {
		if (cm == null) {
			return;
		}
		this.code = cm.getCode();
		this.message = cm.getMessage();
	}

	private Result(CodeMsg cm, String message) {
		if (cm == null) {
			return;
		}
		this.code = cm.getCode();
		this.message = message;
	}

	/**
	 * 成功时候的调用
	 *
	 * @return
	 */
	public static <T> Result<T> success(T data) {
		return new Result<T>(data);
	}

	/**
	 * 成功，不需要传入参数
	 *
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static <T> Result<T> success() {
		return (Result<T>) success("");
	}

	/**
	 * 失败时候的调用
	 *
	 * @return
	 */
	public static <T> Result<T> error(CodeMsg cm) {
		return new Result<T>(cm);
	}

	/**
	 * 失败时候的调用,扩展消息参数
	 *
	 * @param cm
	 * @param msg
	 * @return
	 */
	public static <T> Result<T> error(CodeMsg cm, String msg) {
		return new Result<T>(cm,msg);
	}

	public T getData() {
		return data;
	}

	public String getMessage() {
		return message;
	}

	public int getCode() {
		return code;
	}
}
